package com.sso.controller;

import com.sso.pojo.User;
import com.sso.utils.LoginCacheUtil;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpSession;

/**
 * @author xiaoer
 * @date 2020/11/18 14:01
 */
@Controller
@RequestMapping("/view")
public class ViewController {

    @GetMapping("")
    public String toLogin(@RequestParam(required = false, defaultValue = "") String target, HttpSession session, @CookieValue(required = false,value = "TOKEN")Cookie cookie){
        if (StringUtils.isEmpty(target)){
            target = "http://localhost:8002/view";
        }
        //重定向
        if (cookie!=null){
            String token = cookie.getValue();
            User user = LoginCacheUtil.loginUser.get(token);
            if(user!=null){
                return "redirect:"+target;
            }
        }
        session.setAttribute("target",target);
        return "login";
    }
}
